package com.h3c.gm.controller

import com.google.common.collect.Lists
import com.h3c.gm.common.Response
import com.h3c.gm.common.utils.ControllerUtils
import com.h3c.gm.service.BusUserService
import org.apache.commons.lang3.StringUtils
import org.joda.time.DateTime
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

import javax.annotation.Resource

/**
 * Created by d14287 on 2017/6/26.
 */
@RestController
class BusUserController {
    @Resource
    BusUserService busUserService
    /**
     * 获取用户总数
     * @return
     */
    @RequestMapping(value = 'user/count', method = RequestMethod.GET)
    def getCount() {
        def response = new Response()
        response.ofSuccess(busUserService.getCount())
    }
    /**
     * 获取组织的用户总数
     * @return
     */
    @RequestMapping(value = 'user/count/{orgkey}', method = RequestMethod.GET)
    def getOrgCount(@PathVariable(name = 'orgkey') String orgkey) {
        def response = new Response()
        response.ofSuccess(busUserService.getOrgCount(orgkey))
    }

    /**
     * 获取省份用户数
     * @param province
     * @return
     */
    @RequestMapping(value = 'user/procount/{province}', method = RequestMethod.GET)
    def getProCount(@PathVariable(name = 'province') String province) {
        def response = new Response()
        response.ofSuccess(busUserService.getProCount(province))
    }
    /**
     * 按月统计最近12个月新增用户数
     * @return
     */
    @RequestMapping(value = 'user/monthstatistics', method = RequestMethod.GET)
    def getStatistics() {
        def response = new Response()
        def to = DateTime.now().dayOfMonth().withMaximumValue()
        def from = to.minusMonths(11).dayOfMonth().withMinimumValue()
        def contentList = Lists.newArrayList()
        contentList.add('n')
        def initDate = ControllerUtils.initMonthStatistics(from, to, contentList)
        List<Map<String, Object>> dataList = busUserService.getStatistics(from.toDate(), to.toDate())
        dataList.each {
            item ->
                String dateStr = String.valueOf(item.get('m'))
                for (int i = 0; i <= initDate.size(); i++) {
                    String initDateStr = String.valueOf(initDate.get(i).get('m'))
                    if (dateStr.equals(initDateStr)) {
                        initDate.set(i, item)
                        break
                    }
                }
        }
        response.ofSuccess(initDate)
    }
    /**
     * 按省份按月统计最近12个月新增用户数
     * @return
     */
    @RequestMapping(value = 'user/promonthstatistics/{province}', method = RequestMethod.GET)
    def getproStatistics(@PathVariable(name = 'province') String province) {
        def response = new Response()
        def to = DateTime.now().dayOfMonth().withMaximumValue()
        def from = to.minusMonths(11).dayOfMonth().withMinimumValue()
        def contentList = Lists.newArrayList()
        contentList.add('n')
        def initDate = ControllerUtils.initMonthStatistics(from, to, contentList)
        List<Map<String, Object>> dataList = busUserService.getProStatistics(from.toDate(), to.toDate(), province)
        dataList.each {
            item ->
                String dateStr = String.valueOf(item.get('m'))
                for (int i = 0; i <= initDate.size(); i++) {
                    String initDateStr = String.valueOf(initDate.get(i).get('m'))
                    if (dateStr.equals(initDateStr)) {
                        initDate.set(i, item)
                        break
                    }
                }
        }
        response.ofSuccess(initDate)
    }


}
